/**
 * (C) Copyright 2019 Ymatou (http://www.ymatou.com/).
 *
 * All rights reserved.
 */
package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.activitymgt.parameter.AddactivityBean;
import com.shop2cn.iapi.prodstock.parameter.OrderReturnCatalogDto;
import com.shop2cn.iapi.prodstock.parameter.PreOrderReduceCatalogDto;
import com.shop2cn.iapi.prodstock.parameter.ReduceStockListForPreOrderBean;
import com.shop2cn.iapi.prodstock.parameter.ReturnProductActivityStockBean;
import com.shop2cn.iapi.prodstock.parameter.ReturnStockForOrderBean;
import com.shop2cn.iapi.prodstock.service.ReduceStockListForPreOrderCall;
import com.shop2cn.iapi.prodstock.service.ReturnProductActivityStockCall;
import com.shop2cn.iapi.prodstock.service.ReturnStockForOrderCall;
import com.shop2cn.iapi.prodstockmgr.parameter.AddExportTaskBean;
import com.shop2cn.iapi.prodstockmgr.parameter.AddImportTaskBean;
import com.shop2cn.iapi.prodstockmgr.parameter.CatalogDto;
import com.shop2cn.iapi.prodstockmgr.parameter.ExecuteBean;
import com.shop2cn.iapi.prodstockmgr.parameter.GetInventoryCheckingListBean;
import com.shop2cn.iapi.prodstockmgr.parameter.GetStockInInventoryListBean;
import com.shop2cn.iapi.prodstockmgr.parameter.GetStockOutInventoryListBean;
import com.shop2cn.iapi.prodstockmgr.parameter.GetTaskListBean;
import com.shop2cn.iapi.prodstockmgr.parameter.StockInBean;
import com.shop2cn.iapi.prodstockmgr.parameter.StockOutBean;
import com.shop2cn.iapi.prodstockmgr.parameter.StockOutOnDeliveryBean;
import com.shop2cn.iapi.prodstockmgr.service.AddExportTaskCall;
import com.shop2cn.iapi.prodstockmgr.service.AddImportTaskCall;
import com.shop2cn.iapi.prodstockmgr.service.ExecuteCall;
import com.shop2cn.iapi.prodstockmgr.service.GetInventoryCheckingListCall;
import com.shop2cn.iapi.prodstockmgr.service.GetStockInInventoryListCall;
import com.shop2cn.iapi.prodstockmgr.service.GetStockOutInventoryListCall;
import com.shop2cn.iapi.prodstockmgr.service.GetTaskListCall;
import com.shop2cn.iapi.prodstockmgr.service.StockInCall;
import com.shop2cn.iapi.prodstockmgr.service.StockOutCall;
import com.shop2cn.iapi.prodstockmgr.service.StockOutOnDeliveryCall;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymatou.iapi.prodstock.parameter.PreOrderReturnCatalogDto.CatalogListDto;
import com.ymt.core.tool.Logger;
import com.ymttest.database.sqlwapper.GodProductActivityStockWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Shop2cnStockCallService {
	
	
	/**
	 * 活动结束返库存
	 * @return 
	 * @throws Exception 
	 */
	public static void ReturnProductActivityStock(int activityId,AddactivityBean initProductaddactivity) throws Exception{
		String productId = initProductaddactivity.getProductList().get(0).getProductId();
		int rootUserId = initProductaddactivity.getSellerId();
		int ProductInActivityId = new GodProductActivityStockWapper().selectProductInActivityId(productId, activityId);
		ReturnProductActivityStockBean returnproductactivitystockBean = new ReturnProductActivityStockBean();
		ReturnProductActivityStockCall returnproductactivitystockCall = new ReturnProductActivityStockCall();
		
		returnproductactivitystockBean.setActivityID(activityId);
		returnproductactivitystockBean.setProductID(productId);
		returnproductactivitystockBean.setProductActivityId(ProductInActivityId);
		returnproductactivitystockBean.setActivityType(initProductaddactivity.getActivityType());
		returnproductactivitystockBean.setUserID(rootUserId);
		returnproductactivitystockBean.setBeginTime(YMTDateUtil.getFormatDateString(initProductaddactivity.getBeginTime()));
		returnproductactivitystockBean.setEndTime(YMTDateUtil.getFormatDateString(initProductaddactivity.getEndTime()));
		
		returnproductactivitystockCall.setData(returnproductactivitystockBean);
		returnproductactivitystockCall.callService();			
		
	}
	
	
	/**
	 * 执行任务列表
	 * @return 
	 * @throws Exception 
	 */
	public static void Execute() throws Exception{
		ExecuteBean executeBean = new ExecuteBean();
		ExecuteCall executeCall = new ExecuteCall();
		executeCall.setData(executeBean);
		executeCall.callService();		
		Logger.verifyEquals("ok", executeCall.getReturnData(), "验证code");
	}
	
	/**
	 * 获取任务列表
	 * @return 
	 * @throws Exception 
	 */
	public static JsonArray GetTaskList(int mchid,int type) throws Exception{
		GetTaskListBean gettasklistBean = new GetTaskListBean();
		GetTaskListCall gettasklistCall = new GetTaskListCall();
		gettasklistBean.setSellerId(mchid);
		gettasklistBean.setType(type);
		gettasklistBean.setPageIndex(1);
		gettasklistBean.setPageSize(10);
		gettasklistCall.setData(gettasklistBean);
		gettasklistCall.callService();
		Logger.verifyEquals("200", gettasklistCall.getString("code"), "验证code");
		return gettasklistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("taskList");
	}
	
	
	
	/**
	 * 创建一个导出任务
	 * @throws Exception 
	 */
	public static void AddExportTask(int mchid) throws Exception{
		AddExportTaskBean addexporttaskBean = new AddExportTaskBean();
		AddExportTaskCall addexporttaskCall = new AddExportTaskCall();
		addexporttaskBean.setSellerId(mchid);
		addexporttaskCall.setData(addexporttaskBean);
		addexporttaskCall.callService();
		Logger.verifyEquals("200", addexporttaskCall.getString("code"), "验证code");
	}
	
	/**
	 * 随便导入一个商品
	 * @throws Exception 
	 */
	public static void AddImportTask(int mchid) throws Exception{
		AddImportTaskBean addimporttaskBean = new AddImportTaskBean();
		AddImportTaskCall addimporttaskCall = new AddImportTaskCall();
		addimporttaskBean.setFileUrl("http://file.iapi.ymatou.com/G01/M00/7A/21/rBBlD15xxTSAU09nAAAOv7_F3SQ04.xlsx");
		addimporttaskBean.setSellerId(mchid);
		addimporttaskBean.setFileName("aa");
		addimporttaskCall.setData(addimporttaskBean);
		addimporttaskCall.callService();
		Logger.verifyEquals("200", addimporttaskCall.getString("code"), "验证code");
	}

	/**
	 * 库存清单列表
	 * 
	 * @param user
	 * @param productId
	 * @param num
	 * @return
	 * @throws IOException
	 */
	public static JsonArray GetInventoryCheckingList(int sellerId, String productName) throws IOException {
		GetInventoryCheckingListBean getinventorycheckinglistBean = new GetInventoryCheckingListBean();
		GetInventoryCheckingListCall getinventorycheckinglistCall = new GetInventoryCheckingListCall();
		getinventorycheckinglistBean.setProductName(productName);
		getinventorycheckinglistBean.setSellerId(sellerId);
		getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
		getinventorycheckinglistCall.callService();
		Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
		JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
		return catalogList;
	}
	
	/**
	 * 获取入库单列表
	 * @param rootUserId
	 * @return 
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static JsonArray GetStockInInventoryList(int rootUserId) throws ClientProtocolException, IOException{
		GetStockInInventoryListBean getstockininventorylistBean = new GetStockInInventoryListBean();
		GetStockInInventoryListCall getstockininventorylistCall = new GetStockInInventoryListCall();
		getstockininventorylistBean.setSellerId(rootUserId);
		getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
		getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
		getstockininventorylistBean.setPageIndex(1);
		getstockininventorylistBean.setPageSize(50);
		getstockininventorylistCall.setData(getstockininventorylistBean);
		getstockininventorylistCall.callService();			
		Logger.verifyEquals("200", getstockininventorylistCall.getString("code"), "验证code");
		Logger.verifyEquals(true, getstockininventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
		
		JsonArray asJsonArray = getstockininventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
		return asJsonArray;
	}
	
	/**
	 * 获取出库单列表
	 * @param rootUserId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static JsonArray GetStockOutInventoryList(int rootUserId) throws ClientProtocolException, IOException{
		GetStockOutInventoryListBean getstockoutinventorylistBean = new GetStockOutInventoryListBean();
		GetStockOutInventoryListCall getstockoutinventorylistCall = new GetStockOutInventoryListCall();
		getstockoutinventorylistBean.setSellerId(rootUserId);
		getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
		getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
		getstockoutinventorylistBean.setType(0);
		
		getstockoutinventorylistBean.setType(0);
		getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
		getstockoutinventorylistCall.callService();			
		Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
		Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
		
		JsonArray asJsonArray = getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
		return asJsonArray;
	}

	// 发货出库
	public static long stockOutOnDelivery(int user, String productId, int num) throws IOException {
		StockOutOnDeliveryBean stockoutondeliveryBean = new StockOutOnDeliveryBean();
		StockOutOnDeliveryCall stockoutondeliveryCall = new StockOutOnDeliveryCall();
		ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(user, productId);
		long orderId = Long.parseLong(TestDataManager.getRandomNum(10));
		stockoutondeliveryBean.setOrderId(orderId);
		CatalogDto ca = new CatalogDto();
		ca.setNum(num);
		stockoutondeliveryBean.setSellerId(user);
		ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
		ca.setProductId(productId);
		stockoutondeliveryBean.setCatalogList(Arrays.asList(ca));
		stockoutondeliveryCall.setData(stockoutondeliveryBean);
		stockoutondeliveryCall.callService();
		return orderId;
	}
	
	// 发货出库
	public static long stockOutOnDelivery(int user, String productId, int num,long orderId) throws IOException {
		StockOutOnDeliveryBean stockoutondeliveryBean = new StockOutOnDeliveryBean();
		StockOutOnDeliveryCall stockoutondeliveryCall = new StockOutOnDeliveryCall();
		ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(user, productId);
		stockoutondeliveryBean.setOrderId(orderId);
		CatalogDto ca = new CatalogDto();
		ca.setNum(num);
		stockoutondeliveryBean.setSellerId(user);
		ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
		ca.setProductId(productId);
		stockoutondeliveryBean.setCatalogList(Arrays.asList(ca));
		stockoutondeliveryCall.setData(stockoutondeliveryBean);
		stockoutondeliveryCall.callService();
		return orderId;

	}

	// 手动出库
	public static String stockOut(int user, String productId, int num, int... type) throws IOException {
		ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(user, productId);
		StockOutBean stockoutBean = new StockOutBean();
		StockOutCall stockoutCall = new StockOutCall();
		CatalogDto catalog = new CatalogDto();

		stockoutBean.setSellerId(user);
		stockoutBean.setRemark("手动减少五件货");
		catalog.setProductId(productId);
		catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
		catalog.setNum(10);
		// 0手动出库，1调整出库 新增 20200211 wuwen
		if (type.length > 0)
			stockoutBean.setType(type[0]);

		stockoutBean.setCatalogList(Arrays.asList(catalog));
		stockoutCall.setData(stockoutBean);
		stockoutCall.callService();

		Logger.verifyEquals("200", stockoutCall.getString("code"), "验证code");
		return stockoutCall.getJsonObject().getAsJsonObject("data").get("inventoryNo").getAsString();
	}

	// 手动入库
	public static void prodstockstockIn(int user, String productId, int num) throws IOException {

		ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(user, productId);
		com.shop2cn.iapi.prodstock.parameter.StockInBean stockinBean = new com.shop2cn.iapi.prodstock.parameter.StockInBean();
		com.shop2cn.iapi.prodstock.service.StockInCall stockinCall = new com.shop2cn.iapi.prodstock.service.StockInCall();

		stockinBean.setInventoryNo("出库单" + TestDataManager.getRandomNum(10));
		CatalogListDto ca = new CatalogListDto();
		ca.setBuyerId(user);
		ca.setStockNum(num);
		ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
		ca.setProductId(productId);
		stockinBean.setCatalogList(Arrays.asList(ca));
		stockinCall.setData(stockinBean);
		stockinCall.callService();
		Logger.verifyEquals("200", stockinCall.getString("Code"), "验证Code");

	}

	// 手动入库
	public static String stockIn(int user, String productId, int num, int... type) throws IOException {

		ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(user, productId);
		StockInBean stockinBean = new StockInBean();
		stockinBean.setSellerId(user);
		stockinBean.setRemark("手动添加五件货");
		CatalogDto catalog = new CatalogDto();
		catalog.setProductId(productId);
		catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
		catalog.setNum(num);
		catalog.setCost(new BigDecimal(10));
		// 0手动入库，1调整库存 新增 20200211 wuwen
		if (type.length > 0)
			stockinBean.setType(type[0]);

		stockinBean.setCatalogList(Arrays.asList(catalog));
		StockInCall stockinCall = new StockInCall();
		stockinCall.setData(stockinBean);
		stockinCall.callService();
		Logger.verifyEquals("200", stockinCall.getString("code"), "验证code");
		return stockinCall.getJsonObject().getAsJsonObject("data").get("inventoryNo").getAsString();
	}

	
	
	
	// 下单扣减库存
			public static long InitReduceStockForOrder(int user, String catalogId, String productId, int num)
					throws IOException {
				List<PreOrderReduceCatalogDto> catalogStockList = new ArrayList<PreOrderReduceCatalogDto>();
				PreOrderReduceCatalogDto preOrderReduceCatalogDto = new PreOrderReduceCatalogDto();
				long order = Long.parseLong(TestDataManager.getRandomNum(10));
				preOrderReduceCatalogDto.setPreOrderId(order);
				preOrderReduceCatalogDto.setActivityId(0);
				preOrderReduceCatalogDto.setBuyerId(AgentUsers.csId1);
				preOrderReduceCatalogDto.setCatalogId(catalogId);
				preOrderReduceCatalogDto.setCatalogType(0);
				preOrderReduceCatalogDto.setProductId(productId);
				preOrderReduceCatalogDto.setSellerId(user);
				preOrderReduceCatalogDto.setBuyCount(num);
				preOrderReduceCatalogDto.setProductLimitNumber(0);
				preOrderReduceCatalogDto.setProductLimitStartTime("0001-01-01T00:00:00");
				preOrderReduceCatalogDto.setActivityLimitNumber(0);
				catalogStockList.add(preOrderReduceCatalogDto);

				ReduceStockListForPreOrderBean reducestocklistforpreorderBean = new ReduceStockListForPreOrderBean();
				ReduceStockListForPreOrderCall reducestocklistforpreorderCall = new ReduceStockListForPreOrderCall();
				reducestocklistforpreorderBean.setCatalogList(catalogStockList);
				reducestocklistforpreorderCall.setData(reducestocklistforpreorderBean);
				reducestocklistforpreorderCall.callService();
				Logger.verifyEquals("200", reducestocklistforpreorderCall.getString("Code"), "验证code");

				return order;

			}
			
			
			// 下单活动扣减库存
			public static long InitReduceStockForOrder(int user, int activityId,String catalogId, String productId, int num)
					throws IOException {
				List<PreOrderReduceCatalogDto> catalogStockList = new ArrayList<PreOrderReduceCatalogDto>();
				PreOrderReduceCatalogDto preOrderReduceCatalogDto = new PreOrderReduceCatalogDto();
				long order = Long.parseLong(TestDataManager.getRandomNum(10));
				preOrderReduceCatalogDto.setPreOrderId(order);
				preOrderReduceCatalogDto.setActivityId(activityId);
				preOrderReduceCatalogDto.setBuyerId(AgentUsers.csId1);
				preOrderReduceCatalogDto.setCatalogId(catalogId);
				preOrderReduceCatalogDto.setCatalogType(0);
				preOrderReduceCatalogDto.setProductId(productId);
				preOrderReduceCatalogDto.setSellerId(user);
				preOrderReduceCatalogDto.setBuyCount(num);
				preOrderReduceCatalogDto.setProductLimitNumber(0);
				preOrderReduceCatalogDto.setProductLimitStartTime("0001-01-01T00:00:00");
				preOrderReduceCatalogDto.setActivityLimitNumber(0);
				catalogStockList.add(preOrderReduceCatalogDto);

				ReduceStockListForPreOrderBean reducestocklistforpreorderBean = new ReduceStockListForPreOrderBean();
				ReduceStockListForPreOrderCall reducestocklistforpreorderCall = new ReduceStockListForPreOrderCall();
				reducestocklistforpreorderBean.setCatalogList(catalogStockList);
				reducestocklistforpreorderCall.setData(reducestocklistforpreorderBean);
				reducestocklistforpreorderCall.callService();
				Logger.verifyEquals("200", reducestocklistforpreorderCall.getString("Code"), "验证code");

				return order;

			}
		
		
		// 取消订单返还库存
		public static void ReturnStockForOrder(int user, String catalogId, String productId, long order)
				throws IOException {
			ReturnStockForOrderBean returnstockfororderBean = new ReturnStockForOrderBean();
			ReturnStockForOrderCall returnstockfororderCall = new ReturnStockForOrderCall();
			
			returnstockfororderBean.setOrderId(order);
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(order);
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(user);
			orderReturnCatalogDto.setStockNum(10);
			returnstockfororderBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfororderCall.setData(returnstockfororderBean);
			returnstockfororderCall.callService();		
			
		}
		
}
